package com.cangmaomao.m_mePen.presenter

import com.cangmaomao.lib.config.SUCCESS
import com.cangmaomao.m_mePen.api.ConfigApi
import com.cangmaomao.m_mePen.bean.Login
import com.cangmaomao.m_mePen.contract.LoginContract
import com.cangmaomao.network.request.HttpManage
import com.cangmaomao.network.request.utils.RxSchedulers
import io.reactivex.disposables.Disposable

class LoginPresenter(val view: LoginContract.View) : LoginContract.Presenter {

    override fun start() {
        loadLogin()
    }


    init {
        view.setPresenter(this)
    }

    override fun loadLogin(): Disposable {
        return HttpManage.getInstance()
                .loadingView(view.layoutView(), true)
                .create(ConfigApi::class.java)
                .login(view.loadMap())
                .compose(RxSchedulers.io_main())
                .subscribe({ handleData(it) }, { view.requestFail(it) })
    }


    override fun handleData(data: Login) {
        HttpManage.getInstance().loadingEnd()
        if (data.msg == SUCCESS)
            view.responseData(true)
        else
            view.responseData(false, data.msg)
    }

}